package com.lyl.servicelive.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.commonutils.R;
import com.lyl.servicelive.entity.Course;
import com.lyl.servicelive.entity.CourseAccount;
import com.lyl.servicelive.entity.CourseConfig;
import com.lyl.servicelive.entity.Visitor;
import com.lyl.servicelive.entity.vo.LiveCourseConfigVo;
import com.lyl.servicelive.entity.vo.LiveCourseFormVo;
import com.lyl.servicelive.entity.vo.LiveCourseVo;
import com.lyl.servicelive.service.CourseAccountService;
import com.lyl.servicelive.service.CourseConfigService;
import com.lyl.servicelive.service.CourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 直播课程表 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/servicelive/course")
public class CourseController {
    @Autowired
    private CourseService liveCourseService;


    @Autowired
    private CourseAccountService  courseAccountService;


    @ApiOperation(value = "获取最近的直播")
    @GetMapping("findLatelyList")
    public R findLatelyList() {
        List<LiveCourseVo> courseVoList=  liveCourseService.findLatelyList();
        return R.ok().data("courseVoList",courseVoList);
    }



    @ApiOperation(value = "获取账号信息")
    @GetMapping("getLiveCourseAccount/{id}")
    public R getLiveCourseAccount(@PathVariable Long id) {
       CourseAccount courseAccount= courseAccountService.getByLiveCourseId(id);
        return R.ok().data("courseAccount",courseAccount);
    }


    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public R get(@PathVariable Long id) {
        Course liveCourse = liveCourseService.getById(id);
        return R.ok().data("liveCourse",liveCourse);
    }

    @ApiOperation(value = "获取直播详细信息")
    @GetMapping("getInfo/{id}")
    public R getInfo(@PathVariable Long id) {
       LiveCourseFormVo courseFormVo= liveCourseService.getLiveCourseFormVo(id);

        return R.ok().data("course",courseFormVo);
    }

    @ApiOperation(value = "修改")
    @PutMapping("update")
    public R updateById(@RequestBody LiveCourseFormVo liveCourseVo) {
      if(  liveCourseService.updateById(liveCourseVo)) {
          return R.ok().message("修改成功");
      }else{
          return R.error().message("修改失败");
      }
    }




    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public R index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<Course> pageParam = new Page<>(page, limit);
        IPage<Course> pageModel = liveCourseService.selectPage(pageParam);
        return R.ok().data("courseList",pageModel);
    }


    @ApiOperation(value = "新增")
    @PostMapping("save")
    public R save(@RequestBody LiveCourseFormVo liveCourseVo) {
        if(liveCourseService.save(liveCourseVo)){
            return R.ok().message("新增成功");
        }else{
            return R.error().message("新增失败");
        }

    }

    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
      if(  liveCourseService.removeLive(id)) {
          return R.ok().message("移除成功");
      }else{
          return R.error().message("移除失败,请稍后再试");
      }
    }

}

